package com.baic.ibase.controller.api.v1;


import com.baic.ibase.models.dao.Roles;
import com.baic.ibase.models.req.common.ApiResponse;
import com.baic.ibase.models.req.common.IdRequest;
import com.baic.ibase.models.req.common.PageRequest;
import com.baic.ibase.models.req.roles.CreateRoleReq;
import com.baic.ibase.models.req.roles.DeleteRoleReq;
import com.baic.ibase.service.roles.RolesService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/api/v1/roles")
public class RoleController {

    @Autowired
    private RolesService rolesService;

    /**
     * @api {POST} /api/v1/roles/ListRoles 查询角色
     * @apiVersion 1.0.0
     * @apiGroup RoleController
     * @apiName ListRolesPage()
     * @apiParam (请求体) {Number} pageNum
     * @apiParam (请求体) {Number} pageSize
     * @apiParam (请求体) {String} sort
     * @apiParam (请求体) {String} order
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"aAQLHz","pageSize":587,"sort":"h1vcLqe","pageNum":6038,"order":"cZB"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Array} data.records
     * @apiSuccess (响应结果) {Number} data.records.id
     * @apiSuccess (响应结果) {String} data.records.roleName 角色名称
     * @apiSuccess (响应结果) {String} data.records.roleCode 角色编码
     * @apiSuccess (响应结果) {String} data.records.description 描述
     * @apiSuccess (响应结果) {Object} data.records.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.records.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.records.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.records.updatedUserId 更新人
     * @apiSuccess (响应结果) {Number} data.total
     * @apiSuccess (响应结果) {Number} data.size
     * @apiSuccess (响应结果) {Number} data.current
     * @apiSuccess (响应结果) {Array} data.orders
     * @apiSuccess (响应结果) {String} data.orders.column
     * @apiSuccess (响应结果) {Boolean} data.orders.asc
     * @apiSuccess (响应结果) {Boolean} data.optimizeCountSql
     * @apiSuccess (响应结果) {Boolean} data.searchCount
     * @apiSuccess (响应结果) {Boolean} data.optimizeJoinOfCountSql
     * @apiSuccess (响应结果) {Number} data.maxLimit
     * @apiSuccess (响应结果) {String} data.countId
     * @apiSuccessExample 响应结果示例
     * {"traceId":"SUkp6BkW","code":2084,"data":{"total":3623,"current":1030,"size":690,"optimizeCountSql":false,"records":[{"createdUserId":8501,"roleCode":"1v","roleName":"Ot","description":"R","id":6524,"updatedUserId":8281}],"maxLimit":6532,"searchCount":false,"optimizeJoinOfCountSql":false,"orders":[{"asc":false,"column":"8RQb0Ko"}],"countId":"RZOZ"},"success":false,"message":"FknVcR","errors":"S"}
     */
    @PostMapping("/ListRoles")
    public ApiResponse<Page<Roles>> ListRolesPage(@RequestBody PageRequest req) {
        Page<Roles> page = rolesService.page(new Page<>(req.getPageNum(), req.getPageSize()));
        return ApiResponse.ok(page);
    }


    /**
     * @api {POST} /api/v1/roles/AddRoles 添加角色
     * @apiVersion 1.0.0
     * @apiGroup RoleController
     * @apiName AddRoles()
     * @apiParam (请求体) {String} roleName
     * @apiParam (请求体) {String} roleCode
     * @apiParam (请求体) {String} description
     * @apiParamExample 请求体示例
     * {"roleCode":"1yqRi7SV","roleName":"iQ5SzeGqJN","description":"og2h7"}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.roleName 角色名称
     * @apiSuccess (响应结果) {String} data.roleCode 角色编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"3vfy","code":8885,"data":{"createdUserId":7076,"roleCode":"u802z","roleName":"Yz","description":"kAudUV","id":6910,"updatedUserId":891},"success":false,"message":"5","errors":"p"}
     */
    @PostMapping("/AddRoles")
    public ApiResponse<Roles> AddRoles(@RequestBody CreateRoleReq  req) {
        log.info("addRoles req:{}", req);
        Roles roles = new Roles();
        roles.setRoleName(req.getRoleName());
        roles.setRoleCode(req.getRoleCode());
        roles.setDescription(req.getDescription());
        roles.setCreatedUserId(1L);
        roles.setUpdatedUserId(1L);
        roles.setCreatedTime(LocalDateTime.now());
        roles.setUpdatedTime(LocalDateTime.now());
        if (rolesService.save(roles)) {
            return ApiResponse.ok(roles);
        } else {
            return ApiResponse.error(500, "添加角色失败");
        }
    }

    /**
     * @api {POST} /api/v1/roles/DeleteRoles 删除角色
     * @apiVersion 1.0.0
     * @apiGroup RoleController
     * @apiName DeleteRoles()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"6z1o49","id":6139}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Boolean} data
     * @apiSuccessExample 响应结果示例
     * {"traceId":"g","code":9886,"data":false,"success":true,"message":"t47R","errors":"4zxKLp1uW"}
     */
    @PostMapping("/DeleteRoles")
    public ApiResponse<Boolean> DeleteRoles(@RequestBody DeleteRoleReq req) {
        log.info("deleteUsers req:{}", req);
        if (rolesService.removeById(req.getId())) {
            return ApiResponse.ok();
        } else {
            return ApiResponse.error(500, "删除失败");
        }
    }


    /**
     * @api {POST} /api/v1/roles/GetRoles 查询角色详情
     * @apiVersion 1.0.0
     * @apiGroup RoleController
     * @apiName GetRoles()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} requestId
     * @apiParamExample 请求体示例
     * {"requestId":"i3QXZeSZi","id":9436}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.roleName 角色名称
     * @apiSuccess (响应结果) {String} data.roleCode 角色编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"h8uL5B","code":6649,"data":{"createdUserId":4155,"roleCode":"QTXeX6wG","roleName":"um5gSv","description":"EuPIg","id":9831,"updatedUserId":7011},"success":false,"message":"ColZjx","errors":"lLbP"}
     */
    @PostMapping("/GetRoles")
    public ApiResponse<Roles> GetRoles(@RequestBody IdRequest req) {
        log.info("getRoles req:{}", req);
        Roles roles = rolesService.getById(req.getId());
        return ApiResponse.ok(roles);
    }

    /**
     * @api {POST} /api/v1/roles/UpdateRoles 更新角色
     * @apiVersion 1.0.0
     * @apiGroup RoleController
     * @apiName UpdateRoles()
     * @apiParam (请求体) {Number} id
     * @apiParam (请求体) {String} roleName 角色名称
     * @apiParam (请求体) {String} roleCode 角色编码
     * @apiParam (请求体) {String} description 描述
     * @apiParam (请求体) {Object} createdTime 创建时间
     * @apiParam (请求体) {Object} updatedTime 更新时间
     * @apiParam (请求体) {Number} createdUserId 创建人
     * @apiParam (请求体) {Number} updatedUserId 更新人
     * @apiParamExample 请求体示例
     * {"createdUserId":2988,"roleCode":"mJwnTyY","roleName":"kIVqoFxi","description":"r8as8","id":1339,"updatedUserId":358}
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {String} errors
     * @apiSuccess (响应结果) {String} traceId
     * @apiSuccess (响应结果) {Boolean} success
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccess (响应结果) {Number} data.id
     * @apiSuccess (响应结果) {String} data.roleName 角色名称
     * @apiSuccess (响应结果) {String} data.roleCode 角色编码
     * @apiSuccess (响应结果) {String} data.description 描述
     * @apiSuccess (响应结果) {Object} data.createdTime 创建时间
     * @apiSuccess (响应结果) {Object} data.updatedTime 更新时间
     * @apiSuccess (响应结果) {Number} data.createdUserId 创建人
     * @apiSuccess (响应结果) {Number} data.updatedUserId 更新人
     * @apiSuccessExample 响应结果示例
     * {"traceId":"7","code":2873,"data":{"createdUserId":9910,"roleCode":"UGkMc","roleName":"r2aB","description":"r7","id":911,"updatedUserId":8143},"success":false,"message":"FO1f7qT6","errors":"zJh6gEP"}
     */
    @PostMapping("/UpdateRoles")
    public ApiResponse<Roles> UpdateRoles(@RequestBody Roles req) {
        log.info("updateRoles req:{}", req);
        req.setUpdatedTime(LocalDateTime.now());
        req.setUpdatedUserId(1L);
        if (rolesService.updateById(req)) {
            return ApiResponse.ok(req);
        } else {
            return ApiResponse.error(500, "更新失败");
        }
    }

}
